{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 第六步：保存/读取模型，预测结果"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1. 导入工具包"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "\n",
    "import seaborn as sns\n",
    "from matplotlib import pyplot as plt\n",
    "\n",
    "from xgboost import XGBClassifier\n",
    "import xgboost as xgb\n",
    "\n",
    "from sklearn.model_selection import GridSearchCV\n",
    "from sklearn.model_selection import StratifiedKFold\n",
    "from sklearn.model_selection import train_test_split\n",
    "\n",
    "from sklearn.metrics import log_loss\n",
    "import pickle\n",
    "\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2.导入数据 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "dpath = \"./data/\"\n",
    "data = pd.read_csv(dpath + \"RentListingInquries_FE_train.csv\")\n",
    "data_test = pd.read_csv(dpath + \"RentListingInquries_FE_test.csv\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "data train:  (16779, 228)\n",
      "data test:  (74659, 227)\n"
     ]
    }
   ],
   "source": [
    "# 取三分之一的数据作为训练集\n",
    "data_train, data_val = train_test_split(data, test_size = 0.66,random_state = 0)\n",
    "print(\"data train: \",data_train.shape)\n",
    "print(\"data test: \",data_test.shape)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3. 训练和保存模型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 构建分类器\n",
    "xgb8 = XGBClassifier(\n",
    "        learning_rate = 0.1,\n",
    "        n_estimators = 195,  # 第五轮调整获取的最优值\n",
    "        max_depth = 4,  # 第二轮调整获取的最优值\n",
    "        min_child_weight = 3,  # 第三轮调整获取的最优值\n",
    "        gamma = 0,\n",
    "        subsample = 0.8,  # 第四轮调整获取的最优值\n",
    "        colsample_bytree = 0.7,  # 第四轮调整获取的最优值\n",
    "        colsample_bylevel = 0.7,\n",
    "        reg_alpha = 1,  # 第六轮调整获取的最优值\n",
    "        reg_lambda = 0.5,  #第七轮调整获取的最优值 \n",
    "        objective = \"multi:softprob\",\n",
    "        nthread = -1, # 多线程\n",
    "        seed = 6)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 分离 X 和 y\n",
    "y_train = data_train[\"interest_level\"]\n",
    "X_train = data_train.drop(\"interest_level\", axis = 1)\n",
    "\n",
    "X_test = data_test"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "XGBClassifier(base_score=0.5, booster='gbtree', colsample_bylevel=0.7,\n",
       "       colsample_bytree=0.7, gamma=0, learning_rate=0.1, max_delta_step=0,\n",
       "       max_depth=4, min_child_weight=3, missing=None, n_estimators=195,\n",
       "       n_jobs=1, nthread=-1, objective='multi:softprob', random_state=0,\n",
       "       reg_alpha=1, reg_lambda=0.5, scale_pos_weight=1, seed=6,\n",
       "       silent=True, subsample=0.8)"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 训练\n",
    "xgb8.fit(X_train, y_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 保存模型\n",
    "pickle.dump(xgb8, open(\"xgb_model.pkl\", 'wb'))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 4.读取模型，进行测试"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 读取模型\n",
    "xgb = pickle.load(open(\"xgb_model.pkl\", 'rb'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "logloss of train is: 0.5275041351646432\n"
     ]
    }
   ],
   "source": [
    "# 查看训练集上的loss\n",
    "train_predprob = xgb.predict_proba(X_train)\n",
    "logloss = log_loss(y_train, train_predprob)\n",
    "\n",
    "#Print model report:\n",
    "print ('logloss of train is:', logloss)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 预测\n",
    "y_test_pred = xgb.predict_proba(X_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 保存结果\n",
    "out_df = pd.DataFrame(y_test_pred)\n",
    "out_df.columns = [\"high\", \"medium\", \"low\"]\n",
    "\n",
    "out_df.to_csv(\"xgb_Rent.csv\", index=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
